package com.ktgj.customer.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.constant.BusinessTypeConstants;
import com.ktgj.common.core.constant.MallStatus;
import com.ktgj.common.core.enums.EmHelper;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.common.core.utils.SnowFlake;
import com.ktgj.common.redis.service.RedisService;
import com.ktgj.customer.domain.*;
import com.ktgj.customer.domain.vo.*;
import com.ktgj.customer.mapper.CouponMapper;
import com.ktgj.customer.mapper.MallOrderRefundMapper;
import com.ktgj.customer.mapper.MallProductSupplierMapper;
import com.ktgj.customer.service.*;
import com.ktgj.pay.api.RemotePayService;
import com.ktgj.pay.api.domain.BankCardApi;
import com.ktgj.pay.api.domain.BankPayInfoApi;
import com.ktgj.pay.api.domain.CustomerApi;
import com.ktgj.pay.api.domain.YeeBaoResultApi;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.MallOrderMapper;

import javax.servlet.http.HttpServletRequest;

/**
 * 订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-05-30
 */
@Service
public class MallOrderServiceImpl implements IMallOrderService 
{
    @Autowired
    private MallOrderMapper mallOrderMapper;
    @Autowired
    private IMallCartService mallCartService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IMallProductService mallProductService;
    @Autowired
    private IMallOrderProductService mallOrderProductService;
    @Autowired
    private IBankCardService bankCardService;
    @Autowired
    private RemotePayService remotePayService;
    @Autowired
    private IMallProductSpecService mallProductSpecService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private IMallCartProductService mallCartProductService;
    @Autowired
    private IMallProductImgService mallProductImgService;
    @Autowired
    private IMallActivityService mallActivityService;
    @Autowired
    private ILoginService loginService;
    @Autowired
    private IDepositBusinessService depositBusinessService;
    @Autowired
    private MallProductSupplierMapper mallProductSupplierMapper;
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private MallOrderRefundMapper mallOrderRefundMapper;

    /**
     * 查询订单
     * 
     * @param orderId 订单ID
     * @return 订单
     */
    @Override
    public MallOrder selectMallOrderById(Long orderId)
    {
        return mallOrderMapper.selectMallOrderById(orderId);
    }

    /**
     * 查询订单列表
     * 
     * @param mallOrder 订单
     * @return 订单
     */
    @Override
    public List<MallOrder> selectMallOrderList(MallOrder mallOrder)
    {
        return mallOrderMapper.selectMallOrderList(mallOrder);
    }

    /**
     * 新增订单
     * 
     * @param mallOrder 订单
     * @return 结果
     */
    @Override
    public int insertMallOrder(MallOrder mallOrder)
    {
        mallOrder.setCreateTime(DateUtils.getNowDate());
        return mallOrderMapper.insertMallOrder(mallOrder);
    }

    /**
     * 修改订单
     * 
     * @param mallOrder 订单
     * @return 结果
     */
    @Override
    public int updateMallOrder(MallOrder mallOrder)
    {
        mallOrder.setUpdateTime(DateUtils.getNowDate());
        return mallOrderMapper.updateMallOrder(mallOrder);
    }

    /**
     * 批量删除订单
     * 
     * @param orderIds 需要删除的订单ID
     * @return 结果
     */
    @Override
    public int deleteMallOrderByIds(Long[] orderIds)
    {
        return mallOrderMapper.deleteMallOrderByIds(orderIds);
    }

    /**
     * 删除订单信息
     * 
     * @param orderId 订单ID
     * @return 结果
     */
    @Override
    public int deleteMallOrderById(Long orderId)
    {
        return mallOrderMapper.deleteMallOrderById(orderId);
    }

    //填写订单
    @Override
    public List<MallCartVO> fillOrder(Long customerId, String openId, List<Long> productId) {
        List<MallCartVO> mallCartVOList = new ArrayList<>();
        List<MallCartVO> mallCartVOS = mallCartService.mallCartList(customerId, openId);
        productId.forEach(item -> {
            mallCartVOS.forEach(items -> {
                MallCartVO cartVO = new MallCartVO();
                if (item.equals(items.getProductId())) {
                    BeanUtils.copyProperties(items, cartVO);
                    mallCartVOList.add(cartVO);
                }
            });
        });
        return mallCartVOList;
    }

    //生成订单
    @Override
    public String createOrder(Customer customer, MallOrder mallOrder) throws Exception {
        CustomerIdVo idVo = new CustomerIdVo();
        idVo.setCustomerId(customer.getCustomerId());
        CustomerIdVo customerVo = customerService.customerId(idVo);
        BigDecimal payAmount = new BigDecimal(0);
        for (MallOrderProduct mallOrderProduct : mallOrder.getOrderProductList()) {
            BigDecimal price = new BigDecimal(0);
            MallProductDetails details = mallProductService.mallProductDetails(mallOrderProduct.getProductId());
            for (MallProductSpecVO specVO : details.getProductSpecList()) {
                if (String.valueOf(specVO.getProductSpecId()).equals(String.valueOf(mallOrderProduct.getProductSpecId()))) {
                    if (customerVo.getLevel().equals("1")) {
                        price = price.add(specVO.getSalePrice());
                    } else {
                        price = price.add(specVO.getMemberPrice());
                    }
                    if (specVO.getStockCount() == 0 && specVO.getStockCount() < mallOrderProduct.getNumber()) {
                        throw new Exception("商品" + details.getProductName() + "库存量不足");
                    }
                }
            }
            price = price.multiply(new BigDecimal(mallOrderProduct.getNumber()));
            payAmount = payAmount.add(price);
        }
        if (mallOrder.getPayAmount().compareTo(payAmount) != 0) {
            throw new Exception("支付价格出现变动");
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        SnowFlake snowFlake = new SnowFlake(12, 12);
        mallOrder.setOrderUniqueCode(String.valueOf(snowFlake.nextId()));
        if (customer.getOpenId() != null)
            mallOrder.setOpenId(customer.getOpenId());
        mallOrder.setJsjId(customer.getCustomerId());
        mallOrder.setStatus(MallStatus.waitPay.getValue());
        mallOrder.setRemarks(String.valueOf(mallOrder.getTypeId()));
        mallOrder.setCreateTime(new Date());
        mallOrder.setUpdateTime(new Date());
        long timeMillis = System.currentTimeMillis();
        timeMillis += 30 * 60 * 1000;
        Date date = new Date(timeMillis);
        mallOrder.setExpiredTime(date);
        mallOrder.setPayAmount(new BigDecimal(0.10));
        mallOrderMapper.insertMallOrder(mallOrder);
        MallCart cart = new MallCart();
        if (customer.getOpenId() != null)
            cart.setOpenId(customer.getOpenId());
        cart.setCustomerId(customer.getCustomerId());
        List<MallCart> mallCarts = mallCartService.selectMallCartList(cart);
        mallOrder.getOrderProductList().forEach(item -> {
            item.setOrderId(mallOrder.getOrderId());
            item.setCreateTime(new Date());
            item.setUpdateTime(new Date());
            mallOrderProductService.insertMallOrderProduct(item);
            if (mallCarts != null && mallCarts.size() > 0) {
                mallCartProductService.deleteMallCartProduct(mallCarts.get(0).getCartId(), item.getProductId());
            }
        });
        return mallOrder.getOrderUniqueCode();
    }

    //商城银行卡支付
    @Override
    public String bankCardPay(MallBankCardPay bankCardPay) throws Exception {
        MallOrder mallOrder = null;
        List<MallOrderProduct> productList = null;
        YeeBaoResultApi yeeBaoResultApi = null;
        String payType = null;
        if (bankCardPay.jsjOrderId != null) {
            mallOrder = mallOrderMapper.queryOrderByOrderUniqueCode(bankCardPay.jsjOrderId);

            if (bankCardPay.amount!=null && bankCardPay.amount.compareTo(new BigDecimal(0)) > 0){
                mallOrder.setPayAmount(mallOrder.getPayAmount().subtract(bankCardPay.amount));
                mallOrder.setReserveMoney(bankCardPay.amount);
                int i = mallOrderMapper.updateMallOrder(mallOrder);
                if (i <= 0){
                    throw new Exception("需改订单预留款失败");
                }

                DepositBusiness depositBusiness = new DepositBusiness();
                depositBusiness.setRemark("商城银行卡支付");
                depositBusiness.setState("false");
                depositBusiness.setBusinessType(EmHelper.businessType.Mall.getValue());
                depositBusiness.setOperatorId(1L);
                depositBusiness.setOrdersourceid(bankCardPay.payType);
                depositBusiness.setCustomerId(mallOrder.getJsjId());
                depositBusiness.setDetailamount(bankCardPay.amount);
                depositBusiness.setChangetype(2l);
                depositBusiness.setLockState("false");
                depositBusiness.setOrderId(Long.valueOf(mallOrder.getOrderUniqueCode()));
                depositBusiness.setDrawState("false");
                Long aLong = depositBusinessService.insertDepositBusiness(depositBusiness);
                if (aLong <= 0){
                    throw new Exception("添加有预留款记录失败");
                }

                boolean coinAudit = depositBusinessService.goldCoinAudit(1L, aLong, "商城银行卡支付");
                if (!coinAudit){
                    throw new Exception("审核失败，请进行人工审核");
                }
            }

            MallOrderProduct mallOrderProduct = new MallOrderProduct();
            mallOrderProduct.setOrderId(mallOrder.getOrderId());
            productList = mallOrderProductService.selectMallOrderProductList(mallOrderProduct);
            CustomerApi customer = new CustomerApi();
            customer.setCustCertificateNo(bankCardPay.custCertificateNo);
            customer.setContractPhoneOne(bankCardPay.contractPhoneOne);
            //银行卡信息
            BankCardApi card = new BankCardApi();
            card.setCardNumber(bankCardPay.cardNumber);
            card.setCardholder(bankCardPay.cardholder);

            String backCard = bankCardService.backCard(bankCardPay.cardNumber);

            card.bankCode = backCard + "CREDIT";
            card.setCreditCardTime(bankCardPay.creditCardTime);
            card.setCvv2(bankCardPay.Cvv2);

            BankPayInfoApi bankPayInfoApi = new BankPayInfoApi();
            bankPayInfoApi.setJsjOrderId(bankCardPay.jsjOrderId);
            bankPayInfoApi.setMoneyAmout(mallOrder.getPayAmount().stripTrailingZeros().toEngineeringString());
            bankPayInfoApi.setCustomer(customer);
            bankPayInfoApi.setCard(card);
            bankPayInfoApi.setPayType(bankCardPay.payType);
            boolean lock = true;
            List<String> keyList = new ArrayList<>();
            for (MallOrderProduct orderProduct : productList) {
                MallProductSpec productSpec = mallProductSpecService.selectMallProductSpecById(orderProduct.getProductSpecId());
                if (productSpec.getStockCount() <= 10 && productSpec.getStockCount() > orderProduct.getNumber()) {
                    String keyStr = productSpec.getProductId() + "_" + productSpec.getProductSpecId();
                    lock = redisService.lock(keyStr, 1, 30);
                    keyList.add(keyStr);
                } else if (productSpec.getStockCount() > 10 && productSpec.getStockCount() < orderProduct.getNumber()) {
                    MallProduct mallProduct = mallProductService.selectMallProductById(productSpec.getProductId());
                    throw new Exception("商品" + mallProduct.getProductName() + "库存不足");
                }
            }
            if (!lock) {
                throw new Exception("其他人下单中，请稍后");
            }
            try {
                if (bankCardPay.type.equals("creditcard")) {
                    payType = BusinessTypeConstants.BankCredit.getValue();
                    yeeBaoResultApi = remotePayService.CreditSale(bankPayInfoApi);
                } else if (bankCardPay.type.equals("debitcard")) {
                    payType = BusinessTypeConstants.BankDebit.getValue();
                    yeeBaoResultApi = remotePayService.DebitSale(bankPayInfoApi);
                } else {
                    throw new Exception("支付类型不明确");
                }
                System.out.println("订单号：" + bankCardPay.jsjOrderId);
                System.out.println("交易流水号：" + yeeBaoResultApi.getTrxId());
            } catch (Exception e) {
                e.printStackTrace();
                throw new Exception("支付异常");
            } finally {
                for (String key : keyList) {
                    redisService.deleteLock(key);
                }
            }
            if (yeeBaoResultApi.getCode().equals("1")) {
                mallOrder.setStatus(MallStatus.payd.getValue());
                mallOrder.setTransactionId(yeeBaoResultApi.getTrxId());
                mallOrder.setPayTime(new Date());
                mallOrder.setUpdateTime(new Date());
                mallOrder.setPayType(payType);
                mallOrderMapper.updateMallOrder(mallOrder);
                for (MallOrderProduct orderProduct : productList) {
                    MallProductSpec productSpec = mallProductSpecService.selectMallProductSpecById(orderProduct.getProductSpecId());
                    if (productSpec.getStockCount() > orderProduct.getNumber()) {
                        productSpec.setStockCount((int) (productSpec.getStockCount() - orderProduct.getNumber()));
                        productSpec.setUpdateTime(new Date());
                        mallProductSpecService.updateMallProductSpec(productSpec);
                    } else {
                        MallProduct mallProduct = mallProductService.selectMallProductById(productSpec.getProductId());
                        throw new Exception("商品" + mallProduct.getProductName() + "库存不足，" + "稍后为您进行退款");
                    }
                }
                return "支付成功";
            } else {
                throw new Exception(yeeBaoResultApi.getErrorMsg());
            }
        } else {
            throw new Exception("订单号为空");
        }
    }

    //后台订单列表
    @Override
    public List<MallOrderBackstage> mallOrderList(MallOrderSearch search)
    {
        if (search.getEndTime() != null && !search.getEndTime().equals(""))
        {
            search.setEndTime(search.getEndTime() + " 23:59:59");
        }
        List<MallOrderBackstage> backstageList = mallOrderMapper.mallOrderList(search);
        if (backstageList != null && backstageList.size() > 0)
        {
            backstageList.forEach(item -> {
                if (item.getTypeId().equals("0"))
                {
                    item.setTypeId("普通订单");
                } else if (item.getTypeId().equals("10"))
                {
                    item.setTypeId("团单");
                } else if (item.getTypeId().equals("20"))
                {
                    item.setTypeId("秒杀单");
                } else if (item.getTypeId().equals("30"))
                {
                    item.setTypeId("商品组合单");
                } else if (item.getTypeId().equals("40"))
                {
                    item.setTypeId("权益兑换");
                } else if (item.getTypeId().equals("90"))
                {
                    item.setTypeId("混合订单");
                }
                Customer customer = customerService.selectCustomerById(item.getCustomerId());
                if (customer.getNickName() != null && !customer.getNickName().equals(""))
                {
                    item.setNickName(customer.getNickName());
                } else
                {
                    item.setNickName("未知");
                }
                if (customer.getCustomerName() != null && !customer.getCustomerName().equals(""))
                {
                    item.setCustomerName(customer.getCustomerName());
                } else
                {
                    item.setCustomerName("未知");
                }
                item.setContractPhoneOne(customer.getContractPhoneOne());
                CustomerIdVo customerIdVo = new CustomerIdVo();
                try
                {
                    customerIdVo.setCustomerId(item.getCustomerId());
                    CustomerIdVo idVo = customerService.customerId(customerIdVo);
                    item.setCardCode(idVo.getCustomerCard());
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
                MallOrderProduct product = new MallOrderProduct();
                product.setOrderId(item.getOrderId());
                List<MallOrderProduct> mallOrderProducts = mallOrderProductService.selectMallOrderProductList(product);
                item.setProductNumber(mallOrderProducts.size());
                if (mallOrderProducts != null && mallOrderProducts.size() > 0)
                {
                    for (MallOrderProduct mallOrderProduct : mallOrderProducts)
                    {
                        MallProduct mallProduct = mallProductService.selectMallProductById(mallOrderProduct.getProductId());
                        MallProductSpec mallProductSpec = mallProductSpecService.selectMallProductSpecById(mallOrderProduct.getProductSpecId());
                        String productName = mallProduct.getProductName() + "·" + mallProductSpec.getSpecName();
                        item.getProductName().add(productName);
                        if (search.getSupplierId() != null && search.getSupplierId().intValue() == mallProduct.getSupplierId().intValue())
                        {
                            MallSupplierProduct mallSupplier = new MallSupplierProduct();
                            mallSupplier.setOrderProductId(mallOrderProduct.getOrderProductId());
                            mallSupplier.setProductName(mallProduct.getProductName());
                            mallSupplier.setSupplierId(mallProduct.getSupplierId());
                            mallSupplier.setExpressNo(mallOrderProduct.getExpressNo());
                            item.getLstSupplierProduct().add(mallSupplier);
                        }

                        MallOrderProductDetails orderProductDetails = new MallOrderProductDetails();
                        orderProductDetails.setOrderProductId(mallOrderProduct.getOrderProductId());
                        orderProductDetails.setProductName(mallProduct.getProductName());
                        orderProductDetails.setSpecName(mallProductSpec.getSpecName());
                        orderProductDetails.setNumber(mallOrderProduct.getNumber());
                        orderProductDetails.setExpressNo(mallOrderProduct.getExpressNo());
                        orderProductDetails.setOriginalPrice(mallProductSpec.getOriginalPrice());
                        if (mallProduct.getSupplierId() != null) {
                            MallProductSupplier mallProductSupplier = mallProductSupplierMapper.selectMallProductSupplierById(Long.valueOf(mallProduct.getSupplierId()));
                            orderProductDetails.setMallProductSupplier(mallProductSupplier);
                        }
                        item.getProductDetails().add(orderProductDetails);
                    }
                }
                if (item.getCoupon() != null && item.getCoupon().getCouponId() != null && !item.getCoupon().getCouponId().equals(0)) {
                    Coupon coupon = couponMapper.selectCouponById(item.getCoupon().getCouponId());
                    BeanUtils.copyProperties(coupon, item.getCoupon());
                }
            });
        }
        if (search.getSupplierId() != null)
        {
            Iterator<MallOrderBackstage> iterator = backstageList.iterator();
            while (iterator.hasNext())
            {
                MallOrderBackstage mallOrderBackstage = iterator.next();
                if (mallOrderBackstage.getLstSupplierProduct() != null && mallOrderBackstage.getLstSupplierProduct().size() == 0)
                {
                    iterator.remove();
                }
            }
        }
        return backstageList;
    }

    //后台订单详情
    @Override
    public MallOrderBackstageDetails orderBackstageDetails(Long orderId) {
        MallOrderBackstageDetails backstageDetails = mallOrderMapper.orderBackstageDetails(orderId);
        if (backstageDetails.getTypeId().equals("0")) {
            backstageDetails.setTypeId("普通订单");
        } else if (backstageDetails.getTypeId().equals("10")) {
            backstageDetails.setTypeId("团单");
        } else if (backstageDetails.getTypeId().equals("20")) {
            backstageDetails.setTypeId("秒杀单");
        } else if (backstageDetails.getTypeId().equals("30")) {
            backstageDetails.setTypeId("商品组合单");
        } else if (backstageDetails.getTypeId().equals("40")) {
            backstageDetails.setTypeId("权益兑换");
        } else if (backstageDetails.getTypeId().equals("90")) {
            backstageDetails.setTypeId("混合订单");
        }
        if (backstageDetails.getRemarks() != null && !backstageDetails.getRemarks().equals("")) {
            if (backstageDetails.getRemarks().equals("0")) {
                backstageDetails.setRemarks("普通订单");
            } else if (backstageDetails.getRemarks().equals("10")) {
                backstageDetails.setRemarks("团单");
            } else if (backstageDetails.getRemarks().equals("20")) {
                backstageDetails.setRemarks("秒杀单");
            } else if (backstageDetails.getRemarks().equals("30")) {
                backstageDetails.setRemarks("商品组合单");
            } else if (backstageDetails.getRemarks().equals("40")) {
                backstageDetails.setRemarks("权益兑换");
            } else if (backstageDetails.getRemarks().equals("90")) {
                backstageDetails.setRemarks("混合订单");
            }
        }
        if (backstageDetails.getStatus().equals("waitPay")) {
            backstageDetails.setStatus("等待支付");
        } else if (backstageDetails.getStatus().equals("cancelPay")) {
            backstageDetails.setStatus("取消支付");
        } else if (backstageDetails.getStatus().equals("failPay")) {
            backstageDetails.setStatus("失败支付");
        } else if (backstageDetails.getStatus().equals("payd")) {
            backstageDetails.setStatus("已支付");
        } else if (backstageDetails.getStatus().equals("cancelled")) {
            backstageDetails.setStatus("已取消");
        } else if (backstageDetails.getStatus().equals("refunding")) {
            backstageDetails.setStatus("退款中");
        } else if (backstageDetails.getStatus().equals("refundSuccess")) {
            backstageDetails.setStatus("退款成功");
        } else if (backstageDetails.getStatus().equals("refundFailed")) {
            backstageDetails.setStatus("退款失败");
        }
        Customer customer = customerService.selectCustomerById(backstageDetails.getCustomerId());
        if (customer.getNickName() != null && !customer.getNickName().equals("")) {
            if (customer.getOpenId() != null && !customer.getOpenId().equals(""))
                backstageDetails.setNickName(customer.getNickName() + " " + customer.getOpenId());
            else
                backstageDetails.setNickName(customer.getNickName());
        } else {
            if (customer.getOpenId() != null && !customer.getOpenId().equals(""))
                backstageDetails.setNickName("未知" + " " + customer.getOpenId());
            else
                backstageDetails.setNickName("未知");
        }
        if (backstageDetails.getActivityId() != null) {
            MallActivity activity = mallActivityService.selectMallActivityById(backstageDetails.getActivityId());
            backstageDetails.setActivityName(activity.getActivityName());
        }
        MallOrderProduct product = new MallOrderProduct();
        product.setOrderId(backstageDetails.getOrderId());
        List<MallOrderProduct> mallOrderProducts = mallOrderProductService.selectMallOrderProductList(product);
        if (mallOrderProducts != null && mallOrderProducts.size() > 0) {
            mallOrderProducts.forEach(item -> {
                MallOrderProductDetails productDetails = new MallOrderProductDetails();
                productDetails.setOrderProductId(item.getOrderProductId());
                MallProductImg mallProductImg = new MallProductImg();
                mallProductImg.setProductId(item.getProductId());
                List<MallProductImg> mallProductImgs = mallProductImgService.selectMallProductImgList(mallProductImg);
                if (mallProductImgs != null && mallProductImgs.size() > 0) {
                    mallProductImgs.forEach(items -> {
                        productDetails.getImgPath().add(items.getImgPath());
                    });
                }
                MallProduct mallProduct = mallProductService.selectMallProductById(item.getProductId());
                productDetails.setProductName(mallProduct.getProductName());
                MallProductSpec mallProductSpec = mallProductSpecService.selectMallProductSpecById(item.getProductSpecId());
                productDetails.setSpecName(mallProductSpec.getSpecName());
                productDetails.setNumber(item.getNumber());
                productDetails.setExpressNo(item.getExpressNo());
                backstageDetails.getProductDetails().add(productDetails);
            });
        }
        return backstageDetails;
    }

    //APP订单列表
    @Override
    public List<WholeMallOrder> wholeMallOrder(Long customerId, String status) {
        List<WholeMallOrder> wholeMallOrders = mallOrderMapper.wholeMallOrder(customerId, status);
        if (wholeMallOrders != null && wholeMallOrders.size() > 0) {
            wholeMallOrders.forEach(item -> {
                MallOrderProduct orderProduct = new MallOrderProduct();
                orderProduct.setOrderId(item.getOrderId());
                List<MallOrderProduct> mallOrderProducts = mallOrderProductService.selectMallOrderProductList(orderProduct);
                mallOrderProducts.forEach(items -> {
                    MallProductImg productImg = new MallProductImg();
                    productImg.setProductId(items.getProductId());
                    List<MallProductImg> mallProductImgs = mallProductImgService.selectMallProductImgList(productImg);
                    mallProductImgs.forEach(imgs -> {
                        if (imgs.getTypeId() == 0)
                            item.getImgPath().add(imgs.getImgPath());
                    });
                    if (mallOrderProducts.size() == 1) {
                        MallProduct mallProduct = mallProductService.selectMallProductById(items.getProductId());
                        item.setProductName(mallProduct.getProductName());
                    }
                });
                item.setNumber(Long.valueOf(mallOrderProducts.size()));
            });
        }
        return wholeMallOrders;
    }

    //APP订单详情
    @Override
    public WholeMallOrderVO mallOrderDetails(Long orderId) {
        WholeMallOrderVO mallOrderVO = mallOrderMapper.mallOrderDetails(orderId);
        MallOrderProduct orderProduct = new MallOrderProduct();
        orderProduct.setOrderId(mallOrderVO.getOrderId());
        List<MallOrderProduct> mallOrderProducts = mallOrderProductService.selectMallOrderProductList(orderProduct);
        mallOrderProducts.forEach(item -> {
            WholeMallOrderSpecVO orderSpecVO = new WholeMallOrderSpecVO();
            MallProductImg productImg = new MallProductImg();
            productImg.setProductId(item.getProductId());
            List<MallProductImg> mallProductImgs = mallProductImgService.selectMallProductImgList(productImg);
            mallProductImgs.forEach(imgs -> {
                if (imgs.getTypeId() == 0) {
                    orderSpecVO.setImgPath(imgs.getImgPath());
                }
            });
            MallProduct mallProduct = mallProductService.selectMallProductById(item.getProductId());
            orderSpecVO.setProductName(mallProduct.getProductName());
            orderSpecVO.setNumber(item.getNumber());
            orderSpecVO.setSalePrice(item.getSalePrice());
            mallOrderVO.getMallOrderSpec().add(orderSpecVO);
        });
        return mallOrderVO;
    }

    /**
     * 商城退单
     * @param orderId
     * @return
     */
    @Override
    public int mallOrderRefund(Long orderId) {
        MallOrderRefund mallOrderRefund = mallOrderRefundMapper.selectMallOrderRefundByOrderId(orderId);
        if (mallOrderRefund != null)
            return 0;
        MallOrder mallOrder = mallOrderMapper.selectMallOrderById(orderId);
        mallOrder.setStatus(MallStatus.refundOrder.getValue());
        mallOrderMapper.updateMallOrder(mallOrder);
        MallOrderRefund refund = new MallOrderRefund();
        refund.setOrderId(mallOrder.getOrderId());
        refund.setCustomerId(mallOrder.getJsjId());
        refund.setStatus(0L);
        refund.setRefundAmount(mallOrder.getPayAmount());
        refund.setCreateTime(new Date());
        return mallOrderRefundMapper.insertMallOrderRefund(refund);
    }
}
